<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'weather.jsp' starting page</title>
  </head>
  
  <body>
  
  <input type="button" id="myBtn" value="getJSON"/>
  
  <script type="text/javascript" src="js/json.js"></script>
  <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
  <script type="text/javascript">
  	var xmlHttp;
	/*创建XMLHttpRequest对象*/
	function createXMLHttpRequest() {
		if(window.ActiveXObject) {
	  		//IE
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		} else {
			//chrome firefox opera 
			xmlHttp = new XMLHttpRequest();
		}
	}
	
	document.getElementById("myBtn").onclick = function(){
		createXMLHttpRequest();
		
		xmlHttp.onreadystatechange = callback;//回调函数

		xmlHttp.open("GET","json.jspx",true);
		xmlHttp.send();
		
	};
	
	function callback() {
		if(xmlHttp.readyState == 4) {
			if(xmlHttp.status == 200) {
				//var json = eval("(" + xmlHttp.responseText + ")");
				var json = JSON.parse(xmlHttp.responseText);
				for(var i = 0;i<json.length;i++){
					var p = json[i];
					alert(p.name);
				}
			} else {
				alert("Ajax Error! code:" + xmlHttp.status);
			}
		}
	}
	
  </script>
  </body>
</html>
